<?php
/**
 * Created by PhpStorm.
 * User: tjjs
 * Date: 17-7-19
 * Time: 上午10:41
 */

namespace ActionsHttp;

use Data\Order_form_DAO;
use Data\Order_for_foods_DAO;

class RestaurantRetreatOrderFormConfirm extends BaseActionHttp
{
    public function doAction() {
        parent::doAction();
        $_SESSION[RESTAURANT_ID]=1;
        // 订单id
        $order_form_id = (int)$this->data[ORDER_FORM_ID];
        // 退单状态
        $retreat_status_res = (int)$this->data[RETREAT_STATUS];


        // 订单记录
        $order_form_record = Order_form_DAO::getRecord($order_form_id);
        if (!$order_form_record) {
            return $this->SendErrMsg("没有该订单记录");
        }

        if ($order_form_record->getIS_ENTRY() == STATE_YES) {
            return $this->SendErrMsg("该订单已入帐，不能退单");
        }
        // 退菜状态
        $retreat_food_status = $order_form_record->getRETREAT_FOOD_STATUS();
        if ($retreat_food_status == RETREAT_FOOD_STATUS_START) {
            return $this->SendErrMsg("有退菜申请发起中，不能退单");
        }

        // 是否成功退过菜
        $order_for_food_list = Order_for_foods_DAO::getRetreatList($order_form_id);
        if ($order_for_food_list) {
            return $this->SendErrMsg("已退过菜，不能进行退单操作");
        }


        // 退单状态
        $retreat_order_status = $order_form_record->getRETREAT_ORDER_STATUS();
        if ($retreat_order_status != RETREAT_ORDER_STATUS_START) {
            return $this->SendErrMsg("没有该退单申请，不能退单");
        }

        // 是否已付款
        $order_form_pay_status = $order_form_record->getPAY_STATUS();
        if($order_form_pay_status != PAY_STATUS_YES) {
            return $this->SendErrMsg("订单未付款，不能退单");
        }
        // 商家id
        $restaurant_id = $order_form_record->getRESTAURANT_ID();
        if ($restaurant_id!=$_SESSION[RESTAURANT_ID]) {
            return $this->SendSessionOut();
        }




        if ($retreat_status_res == RESTAURANT_REFUSE) {
            // 拒绝
            $order_form_record->setRETREAT_ORDER_STATUS(RETREAT_ORDER_STATUS_REFUSE);
            Order_form_DAO::update($order_form_record);
        } elseif ($retreat_status_res == RESTAURANT_CONFIRM) {
            // 同意

            // 退单操作
            try {
                // 事务 开始
                $dbMain = \GatewayWorker\Lib\Db::instance('main');
                $dbMain->beginTrans();

                // 订单信息改为退单
                $order_form_record->setRETREAT_ORDER_STATUS(RETREAT_ORDER_STATUS_SUC);
                Order_form_DAO::update($order_form_record);
                // 订单内商品改为退单
                // 订单中商品信息
                $order_for_food_list = Order_for_foods_DAO::getListByOrderFormIdRecord($order_form_id);
                foreach ($order_for_food_list as $order_for_food_record) {
                    $order_for_food_record->setFOOD_COUNT_LEFT(0);
                    Order_for_foods_DAO::update($order_for_food_record);
                }
                //事务 提交
                $dbMain->commitTrans();

            } catch (\Exception $ex) {
                //出现异常,回滚
                echo "预料外错误~~~~\n";
                echo $ex->getMessage()."\n";
                echo $ex->getTraceAsString()." ~~~ traceString\n";
                echo $ex->getCode()."  ~~code  \n";
                echo $ex->getFile()." ~~ file \n";
                echo $ex->getLine()." ~~ line \n";
                echo $ex->getPrevious() ." ~~ previous \n";
                $dbMain->rollBackTrans();
                // 预料外的错误
                return $this->SendErrMsg("预料外的错误~!");
            }
        }


        // 返回数据
        $this->ResponseDataToCurrentClient();
    }
}